package com.qf.smsplatform.strategy.strategy.impl;

import com.qf.smsplatform.common.constants.CacheConstants;
import com.qf.smsplatform.common.constants.StragetyConstents;
import com.qf.smsplatform.common.model.StandardSubmit;
import com.qf.smsplatform.strategy.client.CacheClient;
import com.qf.smsplatform.strategy.strategy.Strategy;
import com.qf.smsplatform.strategy.util.PushErrorMessageUtil;
import lombok.extern.slf4j.Slf4j;
import org.ansj.splitWord.analysis.BaseAnalysis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * projectName: sms-platform
 *
 * @author: 黄睿武
 * time: 2021/8/13 19:40
 * description:
 */


/**
 * 短信内容敏感词的校验策略
 */
@Service(value = "dirtyWord")
@Slf4j
public class DirtyWordStrategyImpl implements Strategy {

    @Autowired
    private CacheClient cacheClient;
    /**
     * 所有策略模块必须有的一个方法,用于校验或补全短信内容(submit对象)
     *
     * @param submit 短信内容
     * @return true代表通过, false校验未通过
     */
    @Override
    public boolean execute(StandardSubmit submit) {
        log.info("[策略模块-敏感词校验]  开始执行.......");
        String messageContent = submit.getMessageContent();
        String content = BaseAnalysis.parse(messageContent).toStringWithOutNature();
        String[] contents = content.split(",");

        for (String s : contents) {
            String result = cacheClient.getBlackValue(CacheConstants.CACHE_PREFIX_DIRTY_WORDS+s);
            if(result != null && result.equals("1")){
                log.warn("[策略模块-敏感词校验]  含有敏感词:{}.......",s);

                //发日志和报告
                PushErrorMessageUtil.sendLog(submit, StragetyConstents.STRATEGY_ERROR_DIRTY_WORDS);
                Integer isreturnstatus = Integer.valueOf(cacheClient.hget(CacheConstants.CACHE_PREFIX_CLIENT + submit.getClientID(), "isreturnstatus"));
                if(isreturnstatus == 1){
                    PushErrorMessageUtil.sendReport(submit,StragetyConstents.STRATEGY_ERROR_DIRTY_WORDS);
                }
                return false;
            }
        }

        log.info("[策略模块-敏感词校验]  执行成功.......");
        return true;
    }
}
